import axios from 'axios';
import qs from 'qs';
import { message } from 'antd';
import Toast from '../component/toast';
import utils from './utils';
import cookie from 'react-cookies';

axios.defaults.baseUrl = process.env.API_URL;
// axios.defaults.timeout = 10000;//响应时间
axios.defaults.headers.post['Accept'] = '*/*';//配置请求头
axios.defaults.headers['Content-Type'] = 'application/x-www-form-urlencoded;charset=UTF-8';//配置请求头
axios.defaults.transformRequest = [function(data) {
	data = qs.stringify(data);
	return data;
}];

const server = axios.create({
    baseURL: process.env.API_URL, // api的base_url
    // timeout: 15000 // 请求超时时间
}) 

// http://sxhlny.api.y8zh.com/open_api/attach/default/upload    // 对接外平台上传地址

server.interceptors.request.use(
    config => {
        if (!config.noLoading) { //接口传入noLoading为true,则不加载loading
            Toast.loading()
        }
        const timestamp = parseInt(new Date().getTime() / 1000); // 当前时间戳
        const app_key = process.env.NODE_ENV == 'production' ? '80d7eb4b5bc9d07a130489b1462e7eeb' : 'ee931436b2446c104ea03c6f7298c340'
        
        config.data = config.data ? config.data : {}
        config.data.timestamp = timestamp;
        config.data.app_type = '45';
        
        // config.data.app_type = '42';  // 对接外平台app_type 
        let sign_data = config.data;
       
        if (config.type === "upLoad") {
            
            sign_data = {
                file: config.data.file,
                file_type: config.data.file_type,
                // auth_token: config.data.auth_token,
                // app_type: config.data.app_type,
                timestamp: config.data.timestamp,
            }
            let formData = new FormData();
                formData.append('file', config.data.file);
                formData.append('file_type', config.data.file_type);
                // formData.append('auth_token', config.data.auth_token);
                formData.append('timestamp', timestamp);   
            config.headers['Content-Type'] = 'multipart/form-data';
            config.data = formData;
            config.transformRequest = function (data) {
                return data;
            }
        }
//         if(config.type === "export"){
//             config.url = `${config.url}?app_key=${app_key}&timestamp=${timestamp}&sign=${sign}&f=${config.f}`;
//             window.open(config.baseURL+config.url)
//             message.success('导出成功')
//             return
//         }
        const sign = utils.calc_sign(sign_data);
        config.url = `${config.url}?app_key=${app_key}&timestamp=${timestamp}&sign=${sign}`;
        return config
    },
    err => {
        Toast.close();
        message.error('请求超时，请稍后重试', 1);
        Promise.reject(err);
    }
)

server.interceptors.response.use(
    res => {
        Toast.close();
        if (res.data.code === 0) {
            return res;
        } else {
            message.error(res.data.message, 1);
            if (res.data && res.data.re_login === 1) {
                cookie.remove('station_console_setting_web_login');
                sessionStorage.removeItem('urlConfig');
                message.error(res.data.message, 2, () => {
                    window.location.href = '/login'
                })
            }
            return Promise.reject(res.data.message)
        }
    },
    err => {
        Toast.close();
        if (!err.response) {
            message.error('接口请求错误', 1)
        }else{
            switch (err.response.status) {
                case 403:
                    err.message = '当前与用户没有权限访问该接口！'
                    break
                case 404:
                    err.message = `请求地址出错: ${err.response.config.url}`
                    break
                case 408:
                    err.message = '请求超时'
                    break
                case 500:
                    err.message = '服务器内部错误'
                    break
                case 501:
                    err.message = '服务未实现'
                    break
                case 502:
                    err.message = '网关错误'
                    break
                case 503:
                    err.message = '服务不可用'
                    break
                case 504:
                    err.message = '网关超时'
                    break
                case 505:
                    err.message = 'HTTP版本不受支持'
                    break
                default:
            }
            message.error(err.message,1)
        }
        return Promise.reject(err);
    }
)

export default server;